﻿@model LogListModel

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.System.Log").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Log");
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.System.Log")
        </h1>
        <div class="pull-right">
            <button type="button" id="delete-selected" class="btn bg-red">
                <i class="fa fa-trash-o"></i>
                @T("Admin.System.Log.DeleteSelected")
            </button>
            <button type="submit" id="clearall" name="clearall" class="btn bg-red">
                <i class="fa fa-trash-o"></i>
                @T("Admin.System.Log.ClearLog")
            </button>
            @Html.ActionConfirmation("clearall")
        </div>
    </div>

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.NopLabelFor(model => model.CreatedOnFrom)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.NopEditorFor(model => model.CreatedOnFrom)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.NopLabelFor(model => model.CreatedOnTo)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.NopEditorFor(model => model.CreatedOnTo)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.NopLabelFor(model => model.Message)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.NopEditorFor(model => model.Message)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        @Html.NopLabelFor(model => model.LogLevelId)
                                    </div>
                                    <div class="col-md-8">
                                        @Html.NopDropDownList("LogLevelId", Model.AvailableLogLevels)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-8 col-md-offset-4">
                                        <button type="button" id="search-log" class="btn btn-primary btn-search">
                                            <i class="fa fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="log-grid"></div>

                        <script>
                            $(document).ready(function() {
                                $("#log-grid").kendoGrid({
                                    dataSource: {
                                        type: "json",
                                        transport: {
                                            read: {
                                                url: "@Html.Raw(Url.Action("LogList", "Log"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: additionalData
                                            }
                                        },
                                        schema: {
                                            data: "Data",
                                            total: "Total",
                                            errors: "Errors"
                                        },
                                        error: function(e) {
                                            display_kendoui_grid_error(e);
                                            // Cancel the changes
                                            this.cancelChanges();
                                        },
                                        pageSize: @(defaultGridPageSize),
                                        serverPaging: true,
                                        serverFiltering: true,
                                        serverSorting: true
                                    },
                                    pageable: {
                                        refresh: true,
                                        pageSizes: [@(gridPageSizes)],
                                        @Html.Partial("_GridPagerMessages")
                                    },
                                    scrollable: false,
                                    dataBound: onDataBound,
                                    columns: [
                                    {
                                        field: "Id",
                                        headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                                        width: 50
                                    }, {
                                        field: "LogLevel",
                                        title: "@T("Admin.System.Log.Fields.LogLevel")",
                                        width: 100
                                    }, {
                                        field: "ShortMessage",
                                        title: "@T("Admin.System.Log.Fields.ShortMessage")"
                                    }, {
                                        field: "CreatedOn",
                                        title: "@T("Admin.System.Log.Fields.CreatedOn")",
                                        width: 200,
                                        type: "date",
                                        format: "{0:G}"
                                    }, {
                                        field: "Id",
                                        title: "@T("Admin.Common.View")",
                                        width: 50,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: '<a class="btn btn-default" href="View/#=Id#"><i class="fa fa-eye"></i>@T("Admin.Common.View")</a>'
                                    }
                                    ]
                                });
                            });
                        </script>

                        <script type="text/javascript">
                            var selectedIds = [];

                            $(document).ready(function() {
                                //search button
                                $('#search-log').click(function() {
                                    //search
                                    var grid = $('#log-grid').data('kendoGrid');
                                    grid.dataSource.page(1); //new search. Set page size to 1
                                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                                    //clear selected checkboxes
                                    $('.checkboxGroups').attr('checked', false).change();
                                    selectedIds = [];
                                    return false;
                                });

                                $("#@Html.FieldIdFor(model => model.Message)").keydown(function(event) {
                                    if (event.keyCode == 13) {
                                        $("#search-log").click();
                                        return false;
                                    }
                                });

                                //"delete selected" button
                                $('#delete-selected').click(function(e) {
                                    e.preventDefault();

                                    var postData = {
                                        selectedIds: selectedIds
                                    };
                                    addAntiForgeryToken(postData);

                                    $.ajax({
                                        cache: false,
                                        type: "POST",
                                        url: "@(Url.Action("DeleteSelected", "Log"))",
                                        data: postData,
                                        complete: function(data) {
                                            //reload grid
                                            var grid = $('#log-grid').data('kendoGrid');
                                            grid.dataSource.page(1); //new search. Set page size to 1
                                            //grid.dataSource.read(); we already loaded the grid above using "page" function
                                            //clear selected checkboxes
                                            $('.checkboxGroups').attr('checked', false).change();
                                            selectedIds = [];
                                        },
                                        error: function(xhr, ajaxOptions, thrownError) {
                                            alert(thrownError);
                                        },
                                        traditional: true
                                    });
                                    return false;
                                });

                                $('#mastercheckbox').click(function() {
                                    $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                                });

                                //wire up checkboxes.
                                $('#log-grid').on('change', 'input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]', function(e) {
                                    var $check = $(this);
                                    if ($check.is(":checked") == true) {
                                        var checked = jQuery.inArray($check.val(), selectedIds);
                                        if (checked == -1) {
                                            //add id to selectedIds.
                                            selectedIds.push($check.val());
                                        }
                                    } else {
                                        var checked = jQuery.inArray($check.val(), selectedIds);
                                        if (checked > -1) {
                                            //remove id from selectedIds.
                                            selectedIds = $.grep(selectedIds, function(item, index) {
                                                return item != $check.val();
                                            });
                                        }
                                    }
                                    updateMasterCheckbox();
                                });
                            });

                            function additionalData() {
                                var data = {
                                    CreatedOnFrom: $('#@Html.FieldIdFor(model => model.CreatedOnFrom)').val(),
                                    CreatedOnTo: $('#@Html.FieldIdFor(model => model.CreatedOnTo)').val(),
                                    Message: $('#@Html.FieldIdFor(model => model.Message)').val(),
                                    LogLevelId: $('#@Html.FieldIdFor(model => model.LogLevelId)').val()
                                };
                                addAntiForgeryToken(data);
                                return data;
                            }

                            function onDataBound(e) {
                                $('#log-grid input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]').each(function() {
                                    var currentId = $(this).val();
                                    var checked = jQuery.inArray(currentId, selectedIds);
                                    //set checked based on if current checkbox's value is in selectedIds.
                                    $(this).attr('checked', checked > -1);
                                });

                                updateMasterCheckbox();
                            }

                            function updateMasterCheckbox() {
                                var numChkBoxes = $('#log-grid input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]').length;
                                var numChkBoxesChecked = $('#log-grid input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]:checked').length;
                                $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                            }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}